package com.fsharemobile.fragments;

import java.util.ArrayList;
import java.util.List;

import com.fsharemobile.FileModifierActivity;
import com.fsharemobile.R;
import com.fsharemobile.adapter.ExplorerArrayAdapter;
import com.fsharemobile.client.FClient;
import com.fsharemobile.client.File;
import com.fsharemobile.client.FileProxy;
import com.fsharemobile.client.FileProxy.OnGetListCompleted;
import com.fsharemobile.client.UserProxy;
import com.fsharemobile.client.UserProxy.OnLoginCompleted;
import com.fsharemobile.model.Files;
import com.fsharemobile.model.Share;
import com.kenny.mypopupwindow.ActionItem;
import com.kenny.mypopupwindow.QuickAction;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Path.FillType;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;


public class ExplorerFragment extends Fragment implements OnClickListener {
	protected static final int REQUEST_ADD_FOLDER = 0;
	ListView lvEx;
	EditText edtSearch;
	ImageView ivMenu;
	public static QuickAction quickAc;
	ExplorerArrayAdapter explorerAdapter;
	public static List<Files> arrFiles= new ArrayList<Files>();
	public static int IMMENU_CLICK_TIME=0;
	
	//private String currentPath;
	//private String fileId;
	
	private FClient fclient = null;
	List<File> fileList = null;
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
	
		
		// slove a problem all the fields are pushed up and the soft keyboard pops up.
		getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(container==null){
			return null;
		}		
		return (LinearLayout) inflater.inflate(R.layout.fragment_explorer, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
//,..,.,.,.,.,		
		Share share = Share.getInstance();
		this.fclient = share.getFclient();
		//UserProxy userProxy = fclient.getUserProxy();
		
		FileProxy fileProxy = fclient.getFileProxy();

		try {
			fileProxy.getList(0, 0, "home",
					new OnGetListCompleted() {

						@Override
						public void onGetListCompleted(
								int errCode,
								List<File> ifileList) {
							System.out.println(errCode);
							fileList = ifileList;
							onLoadGetList(errCode);
						}
					});
		} catch (Exception ex) {
			System.out.println(ex.toString());
		} finally {

		}

		
//.....		
		//arrFiles=initNewFile();
		//arrFiles=fileList;
		lvEx= (ListView) getActivity().findViewById(R.id.lvExplorer);
		edtSearch= (EditText) getActivity().findViewById(R.id.edt_search);
		ivMenu = (ImageView) getActivity().findViewById(R.id.iv_menu);
		//toast= Toast.makeText(getActivity(), null, Toast.LENGTH_SHORT);
		//explorerAdapter= new ExplorerArrayAdapter(getActivity(), R.layout.my_item_explorer_layout, fileList);
		//lvEx.setAdapter(explorerAdapter);
		initMenu();
		ivMenu.setOnClickListener(ivClick);	
		edtSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
					//toast.setText(s);
					//toast.show();
					
				
				
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
			
	}
	
	private OnClickListener ivClick  = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(ExplorerArrayAdapter.myQuickAc!=null){
				ExplorerArrayAdapter.myQuickAc.dismiss();
				
			}
			if(IMMENU_CLICK_TIME==0){
				quickAc.show(v);
				IMMENU_CLICK_TIME=1;
			}else{
				quickAc.dismiss();
				IMMENU_CLICK_TIME=0;
			}
				
		}
	};
	
	
	
	
	



	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//toast.cancel();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//toast.cancel();
	}

	@Override
	public void setHasOptionsMenu(boolean hasMenu) {
		// TODO Auto-generated method stub
		super.setHasOptionsMenu(hasMenu);
	}
	
	

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		//MenuInflater inflater = getSupportMenuInflater();
	    inflater.inflate(R.menu.menu_fragment_vehicle_list, menu);
	   
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	public List<Files> initNewFile(){
		List<Files> arr= new ArrayList<Files>();
		Files mFile1= new Files();
		mFile1.setmId("01");
		mFile1.setmName("Camera");
		mFile1.setmType("01");
		
		Files mFile2= new Files();
		mFile2.setmId("02");
		mFile2.setmName("My Sub");
		mFile2.setmType("02");
		
		Files mFile3= new Files();
		mFile3.setmId("03");
		mFile3.setmName("New Folder");
		mFile3.setmType("03");
		
		Files mFile4= new Files();
		mFile4.setmId("04");
		mFile4.setmName("None Folder");
		mFile4.setmType("");
		
		arr.add(mFile1);
		arr.add(mFile2);
		arr.add(mFile3);
		arr.add(mFile4);
		
		return arr;
	}
	
	public void removeFileOnClickHandler(View v){
		//File fileRemove= (File) v.getTag();
		//plorerAdapter.remove(fileRemove);
	}
	
	public  void initMenu(){
		
		quickAc = new QuickAction(getActivity(), QuickAction.VERTICAL);
		ActionItem itemNewFol=  new ActionItem(1, "New Folder", getResources().getDrawable(R.drawable.ic_new_folder));
		ActionItem itemUpload= new ActionItem(2, "Upload", getResources().getDrawable(R.drawable.ic_upload));
		quickAc.addActionItem(itemNewFol);
		quickAc.addActionItem(itemUpload);
		quickAc.setOnActionItemClickListener(quickCLickAction);
	}
	
	
	private QuickAction.OnActionItemClickListener quickCLickAction= new QuickAction.OnActionItemClickListener() {
		
		@Override
		public void onItemClick(QuickAction source, int pos, int actionId) {
			// TODO Auto-generated method stub
			
			if(actionId==1){
				Intent addFolerInter = new Intent(getActivity(), FileModifierActivity.class);
				addFolerInter.putExtra("add", 0);
				startActivityForResult(addFolerInter, REQUEST_ADD_FOLDER);
				//Toast.makeText(getActivity(), "add new folder", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getActivity(), "share folder", Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	private void onLoadGetList(int err){
		if(err==0){
			explorerAdapter= new ExplorerArrayAdapter(getActivity(), R.layout.my_item_explorer_layout, fileList);
			lvEx.setAdapter(explorerAdapter);
		}else {
			handleErr(err);
		}
	}
	
	private void handleErr(int errCode){
		Toast.makeText(getActivity(), "errrr", Toast.LENGTH_SHORT).show();
	}
	
	

}
